package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

/**
 * @author mrchen
 * @date 2022/2/16 14:32
 */
@SpringBootTest
public class HotelDocumentDemo {
    RestHighLevelClient client;
    @Autowired
    IHotelService iHotelService;
    @Test
    public void batchAddDocument() throws IOException {
        // 1. 从数据库中查询所有的酒店数据 List<Hotel>
        List<Hotel> list = iHotelService.list();
        // 2. 创建批处理请求对象  BulkRequest
        BulkRequest bulkRequest = new BulkRequest("hotel");
        // 3. 遍历集合
        for (Hotel hotel : list) {
            // 4. 将每条数据  Hotel ==> HotelDoc  封装一个IndexRequest
            HotelDoc hotelDoc = new HotelDoc(hotel);
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(String.valueOf(hotel.getId())); // 文档id
            indexRequest.source(JSON.toJSONString(hotelDoc),XContentType.JSON); // 文档内容
            // 5. 通过bulkRequest . add (indexRequest)
            bulkRequest.add(indexRequest);
        }
        // 6. 调用client . bulk 执行批处理请求
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        // 7. 得到响应结果
        System.out.println(bulk.status());
    }
    @Test
    public void addDocument() throws IOException {
        // 根据id 从数据库中查询数据   36934
        Hotel hotel = iHotelService.getById(36934);
        hotel.setPrice(999);
        // 将hotel 转为 HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 创建请求   IndexRequest
        IndexRequest request = new IndexRequest("hotel");
        //     设置请求参数  索引库名称  id   文档内容
        request.id(String.valueOf(hotel.getId()));
        request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
        // 执行请求   client.index
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 解析结果   IndexResponse
        System.out.println(response.getResult());
    }
    /**
     * GET   索引库/_doc/id
     * @throws IOException
     */
    @Test
    public void getDocumentById() throws IOException {
        // 1. 创建请求
        GetRequest request = new GetRequest("hotel","36934");
        // 2. 执行请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3. 解析结果
        String jsonStr = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(jsonStr, HotelDoc.class);

        System.out.println(hotelDoc);
    }
    @Test
    public void updateById() throws IOException {
        // 1. 创建请求
        UpdateRequest request = new UpdateRequest("hotel","36934");
        request.doc("price","999");
        // 2. 执行请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        // 3. 解析结果
        System.out.println(response.getResult());
    }
    @Test
    public void deleteById() throws IOException {
        // 1. 创建请求
        DeleteRequest deleteRequest = new DeleteRequest("hotel","36934");
        // 2. 执行请求
        DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
        // 3. 解析结果
        System.out.println(response.getResult());
    }
    @BeforeEach
    public void init(){
        client = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("http://192.168.200.130:9200")));
    }
    @AfterEach
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
